package com.goodwill.androidtest.relative_layout_test.views;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;

public class MyRelativeLayout extends RelativeLayout {
    private static final String TAG = MyRelativeLayout.class.getSimpleName();

    public MyRelativeLayout(Context context) {
        super(context);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//
//        if (mDirtyHierarchy) {
//            mDirtyHierarchy = false;
//            sortChildren();
//        }

        int myWidth = -1;
        int myHeight = -1;

        int width = 0;
        int height = 0;

        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        logi("widthMode:" + widthMode);
        logi("widthSize:" + widthSize);

//        // Record our dimensions if they are known;
        if (widthMode != MeasureSpec.UNSPECIFIED) {
            myWidth = widthSize;
        }

        if (heightMode != MeasureSpec.UNSPECIFIED) {
            myHeight = heightSize;
        }

        if (widthMode == MeasureSpec.EXACTLY) {
            width = myWidth;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = myHeight;
        }
//
//        View ignore = null;
//        int gravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;
//        final boolean horizontalGravity = gravity != Gravity.START && gravity != 0;
//        gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
//        final boolean verticalGravity = gravity != Gravity.TOP && gravity != 0;
//
//        int left = Integer.MAX_VALUE;
//        int top = Integer.MAX_VALUE;
//        int right = Integer.MIN_VALUE;
//        int bottom = Integer.MIN_VALUE;
//
//        boolean offsetHorizontalAxis = false;
//        boolean offsetVerticalAxis = false;
//
//        if ((horizontalGravity || verticalGravity) && mIgnoreGravity != View.NO_ID) {
//            ignore = findViewById(mIgnoreGravity);
//        }
//
//        final boolean isWrapContentWidth = widthMode != MeasureSpec.EXACTLY;
//        final boolean isWrapContentHeight = heightMode != MeasureSpec.EXACTLY;
//
//        // We need to know our size for doing the correct computation of children positioning in RTL
//        // mode but there is no practical way to get it instead of running the code below.
//        // So, instead of running the code twice, we just set the width to a "default display width"
//        // before the computation and then, as a last pass, we will update their real position with
//        // an offset equals to "DEFAULT_WIDTH - width".
//        final int layoutDirection = getLayoutDirection();
//        if (isLayoutRtl() && myWidth == -1) {
//            myWidth = DEFAULT_WIDTH;
//        }
//
//        View[] views = mSortedHorizontalChildren;
//        int count = views.length;
//
//        for (int i = 0; i < count; i++) {
//            View child = views[i];
//            if (child.getVisibility() != GONE) {
//                LayoutParams params = (LayoutParams) child.getLayoutParams();
//                int[] rules = params.getRules(layoutDirection);
//
//                applyHorizontalSizeRules(params, myWidth, rules);
//                measureChildHorizontal(child, params, myWidth, myHeight);
//
//                if (positionChildHorizontal(child, params, myWidth, isWrapContentWidth)) {
//                    offsetHorizontalAxis = true;
//                }
//            }
//        }
//
//        views = mSortedVerticalChildren;
//        count = views.length;
//        final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
//
//        for (int i = 0; i < count; i++) {
//            final View child = views[i];
//            if (child.getVisibility() != GONE) {
//                final LayoutParams params = (LayoutParams) child.getLayoutParams();
//
//                applyVerticalSizeRules(params, myHeight, child.getBaseline());
//                measureChild(child, params, myWidth, myHeight);
//                if (positionChildVertical(child, params, myHeight, isWrapContentHeight)) {
//                    offsetVerticalAxis = true;
//                }
//
//                if (isWrapContentWidth) {
//                    if (isLayoutRtl()) {
//                        if (targetSdkVersion < Build.VERSION_CODES.KITKAT) {
//                            width = Math.max(width, myWidth - params.mLeft);
//                        } else {
//                            width = Math.max(width, myWidth - params.mLeft + params.leftMargin);
//                        }
//                    } else {
//                        if (targetSdkVersion < Build.VERSION_CODES.KITKAT) {
//                            width = Math.max(width, params.mRight);
//                        } else {
//                            width = Math.max(width, params.mRight + params.rightMargin);
//                        }
//                    }
//                }
//
//                if (isWrapContentHeight) {
//                    if (targetSdkVersion < Build.VERSION_CODES.KITKAT) {
//                        height = Math.max(height, params.mBottom);
//                    } else {
//                        height = Math.max(height, params.mBottom + params.bottomMargin);
//                    }
//                }
//
//                if (child != ignore || verticalGravity) {
//                    left = Math.min(left, params.mLeft - params.leftMargin);
//                    top = Math.min(top, params.mTop - params.topMargin);
//                }
//
//                if (child != ignore || horizontalGravity) {
//                    right = Math.max(right, params.mRight + params.rightMargin);
//                    bottom = Math.max(bottom, params.mBottom + params.bottomMargin);
//                }
//            }
//        }
//
//        // Use the top-start-most laid out view as the baseline. RTL offsets are
//        // applied later, so we can use the left-most edge as the starting edge.
//        View baselineView = null;
//        LayoutParams baselineParams = null;
//        for (int i = 0; i < count; i++) {
//            final View child = views[i];
//            if (child.getVisibility() != GONE) {
//                final LayoutParams childParams = (LayoutParams) child.getLayoutParams();
//                if (baselineView == null || baselineParams == null
//                        || compareLayoutPosition(childParams, baselineParams) < 0) {
//                    baselineView = child;
//                    baselineParams = childParams;
//                }
//            }
//        }
//        mBaselineView = baselineView;
//
//        if (isWrapContentWidth) {
//            // Width already has left padding in it since it was calculated by looking at
//            // the right of each child view
//            width += mPaddingRight;
//
//            if (mLayoutParams != null && mLayoutParams.width >= 0) {
//                width = Math.max(width, mLayoutParams.width);
//            }
//
//            width = Math.max(width, getSuggestedMinimumWidth());
//            width = resolveSize(width, widthMeasureSpec);
//
//            if (offsetHorizontalAxis) {
//                for (int i = 0; i < count; i++) {
//                    final View child = views[i];
//                    if (child.getVisibility() != GONE) {
//                        final LayoutParams params = (LayoutParams) child.getLayoutParams();
//                        final int[] rules = params.getRules(layoutDirection);
//                        if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) {
//                            centerHorizontal(child, params, width);
//                        } else if (rules[ALIGN_PARENT_RIGHT] != 0) {
//                            final int childWidth = child.getMeasuredWidth();
//                            params.mLeft = width - mPaddingRight - childWidth;
//                            params.mRight = params.mLeft + childWidth;
//                        }
//                    }
//                }
//            }
//        }
//
//        if (isWrapContentHeight) {
//            // Height already has top padding in it since it was calculated by looking at
//            // the bottom of each child view
//            height += mPaddingBottom;
//
//            if (mLayoutParams != null && mLayoutParams.height >= 0) {
//                height = Math.max(height, mLayoutParams.height);
//            }
//
//            height = Math.max(height, getSuggestedMinimumHeight());
//            height = resolveSize(height, heightMeasureSpec);
//
//            if (offsetVerticalAxis) {
//                for (int i = 0; i < count; i++) {
//                    final View child = views[i];
//                    if (child.getVisibility() != GONE) {
//                        final LayoutParams params = (LayoutParams) child.getLayoutParams();
//                        final int[] rules = params.getRules(layoutDirection);
//                        if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_VERTICAL] != 0) {
//                            centerVertical(child, params, height);
//                        } else if (rules[ALIGN_PARENT_BOTTOM] != 0) {
//                            final int childHeight = child.getMeasuredHeight();
//                            params.mTop = height - mPaddingBottom - childHeight;
//                            params.mBottom = params.mTop + childHeight;
//                        }
//                    }
//                }
//            }
//        }
//
//        if (horizontalGravity || verticalGravity) {
//            final Rect selfBounds = mSelfBounds;
//            selfBounds.set(mPaddingLeft, mPaddingTop, width - mPaddingRight,
//                    height - mPaddingBottom);
//
//            final Rect contentBounds = mContentBounds;
//            Gravity.apply(mGravity, right - left, bottom - top, selfBounds, contentBounds,
//                    layoutDirection);
//
//            final int horizontalOffset = contentBounds.left - left;
//            final int verticalOffset = contentBounds.top - top;
//            if (horizontalOffset != 0 || verticalOffset != 0) {
//                for (int i = 0; i < count; i++) {
//                    final View child = views[i];
//                    if (child.getVisibility() != GONE && child != ignore) {
//                        final LayoutParams params = (LayoutParams) child.getLayoutParams();
//                        if (horizontalGravity) {
//                            params.mLeft += horizontalOffset;
//                            params.mRight += horizontalOffset;
//                        }
//                        if (verticalGravity) {
//                            params.mTop += verticalOffset;
//                            params.mBottom += verticalOffset;
//                        }
//                    }
//                }
//            }
//        }
//
//        if (isLayoutRtl()) {
//            final int offsetWidth = myWidth - width;
//            for (int i = 0; i < count; i++) {
//                final View child = views[i];
//                if (child.getVisibility() != GONE) {
//                    final LayoutParams params = (LayoutParams) child.getLayoutParams();
//                    params.mLeft -= offsetWidth;
//                    params.mRight -= offsetWidth;
//                }
//            }
//        }
//
//        setMeasuredDimension(width, height);
    }


    public void logi(String msg) {
        Log.i(TAG, msg);
    }
}
